//
//  PCViewController.m
//  GOGOTravel
//
//  Created by dllo on 16/2/27.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "PCViewController.h"
#import "PICCollectionViewCell.h"
#import "PICmodel.h"
#import "TextCollectionViewCell.h"

@interface PCViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UIView *titleView;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, assign)BOOL isDisappear;
@property (nonatomic, retain) UIView *textView;
@property (nonatomic, retain) BaseCollectionView *collection;
@property (nonatomic, retain) UITapGestureRecognizer *tapTwo;
@property (nonatomic, retain) PICCollectionViewCell *cell;
@property (nonatomic, retain) UICollectionViewFlowLayout *layout;
@property (nonatomic, retain) UICollectionView *textCollection;
@property (nonatomic, assign) NSInteger page;
@end

@implementation PCViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
-(void)dealloc{
    [_titleView release];
    [_button release];
    [_textView release];
    [_collection release];
    [_tapTwo release];
    [_cell release];
    [_label release];
    [_textCollection release];
    [_picFirst release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor blackColor];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    [self createView];
    
    [self creacteCollection];
    _isDisappear = NO;
}

- (void)createView{
    
    self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
    _titleView.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:_titleView];
    [_titleView release];
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(10, 23, 30, 30);
    [_button setBackgroundImage:[UIImage imageNamed:@"iconfont-jiantouzuo-2.png"] forState:UIControlStateNormal];
    
    [_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [_titleView addSubview:_button];
    
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 100)/2, 40, 200, 30)];
    _label.text = @"详情";
    _label.center = _titleView.center;
    _label.textColor = [UIColor whiteColor];
    _label.font = [UIFont systemFontOfSize:18];
    _label.textAlignment = NSTextAlignmentCenter;
    [_titleView addSubview:_label];
    [_label release];
    
    
    self.textView = [[UIView alloc] initWithFrame:CGRectMake(0, 500, self.view.frame.size.width, self.view.frame.size.height)];
    _textView.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:_textView];
    [_textView release];
    
    
}
- (void)creacteCollection{
    
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _layout.minimumInteritemSpacing = 0;
    _layout.minimumLineSpacing = 0;
    _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 60 - (self.view.frame.size.height - 500));
    
    self.collection = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width,self.view.frame.size.height - 60 - (self.view.frame.size.height - 500) ) collectionViewLayout:_layout];
    
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.pagingEnabled = YES;
    [_collection setContentOffset:CGPointMake(self.view.frame.size.width * _row, 60) animated:YES];
    [self.view addSubview:_collection];
    
    [_collection registerClass:[PICCollectionViewCell class] forCellWithReuseIdentifier:@"PICCollectionViewCell1"];
    
    //轻拍两次
    self.tapTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwoAction:)];
//    _tapTwo.numberOfTouchesRequired = 2;
    _tapTwo.numberOfTapsRequired = 2;
    
    
    //创建text的collection
    
    UICollectionViewFlowLayout *layout1 = [[UICollectionViewFlowLayout alloc] init];
    
    layout1.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 500);
    layout1.minimumInteritemSpacing = 0 ;
    layout1.minimumLineSpacing = 0;
    layout1.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout1.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    self.textCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 500) collectionViewLayout:layout1];
    
    [_textCollection setContentOffset:CGPointMake(self.view.frame.size.width * _row, self.view.frame.size.height - 500) animated:YES];
    _textCollection.delegate = self;
    _textCollection.dataSource = self;
    _textCollection.pagingEnabled = YES;
    _textCollection.showsHorizontalScrollIndicator = NO;
    [_textView addSubview:_textCollection];
    [_textCollection release];
    
    [_textCollection registerClass:[TextCollectionViewCell class] forCellWithReuseIdentifier:@"TextCollectionViewCell"];
}

- (void)tapTwoAction:(UITapGestureRecognizer *)tap{
    
    
    [UIView animateWithDuration:0.2 animations:^{
        _layout.itemSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height * 2);
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataModelArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (collectionView == _collection) {
        self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PICCollectionViewCell1" forIndexPath:indexPath];
       
        [_cell addGestureRecognizer:_tapTwo];
        PICmodel *model = _dataModelArr[indexPath.row];
        
        [_cell.image1 sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:PLACE_HLODER]];
        
        return _cell;
        
    }else{
        
        TextCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TextCollectionViewCell" forIndexPath:indexPath];
        
        PICmodel *model = _dataModelArr[indexPath.row];
        cell.backgroundColor = [UIColor blackColor];
        cell.model = model;
       
        return cell;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    _textCollection.contentOffset = _collection.contentOffset;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_isDisappear == NO) {
        [UIView animateWithDuration:0.2 animations:^{
            _titleView.frame = CGRectMake(0, -60, self.view.frame.size.width, 60);
            
            _textView.alpha = 0;
        }];
        
        
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            _titleView.frame = CGRectMake(0, 0, self.view.frame.size.width, 60);
            _textView.alpha = 1;
        }];
    }
    
    _isDisappear = !_isDisappear;
    
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
   
}
- (void)buttonAction:(UIButton *)button{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
